using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Audio;
using ProtectThePie;
using ThreeThingGame;

namespace ProtectThePieWP7.AdditionalClasses
{
    public enum MyButtonState
    {
        idle,
        pressed
    }

    public class Button
    {
        private MyButtonState state;
        private Rectangle rectangle;
        private Texture2D texture;
        private SoundEffect clickSound;

        public Button(Rectangle rectangle, Texture2D texture, SoundEffect clickSound)
        {           
            this.rectangle = rectangle;
            this.texture = texture;
            this.clickSound = clickSound;
        }
        
        public bool Update(TouchCollection touches)
        {
            foreach (TouchLocation tl in touches)
            {
                if (rectangle.Contains((int)tl.Position.X, (int)tl.Position.Y))
                {
                    state = MyButtonState.pressed;

                    switch (tl.State)
                    {
                        case TouchLocationState.Pressed:
                            Game1.soundEngine.PlaySoundEffect(clickSound);
                            break;
                        case TouchLocationState.Released:
                            return true;
                    }
                }
            }
            return false;
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            switch (state)
            {
                case MyButtonState.idle:
                    spriteBatch.Draw(texture, rectangle, Color.White);
                    break;
                case MyButtonState.pressed:
                    spriteBatch.Draw(texture, 
                        new Rectangle(rectangle.X + 5, rectangle.Y + 5, rectangle.Width, rectangle.Height),
                        new Color(192, 192, 192));
                    state = MyButtonState.idle;
                    break;
            }           
        }
    }
}
